% Scripts to create initila data base for Google App Engine
% Dst (USGS and Kyoto)
% IMF (SWPC)
% F107 (SWPC)

% Get teh Kyoto data

% A part of the text from IAGA document about time stamp convention
% The lAGA resolution from Canberra (1979) states:
% The first value within an hour is labeled  00:01 and is the mean calculated from
% 00:00:30 to 00:01:30 . It follows from this that the first hour of a day is labeled 00.
% The hourly mean value is the mean of the values taken at 01m 60m, or the
% correspondingly filtered value
% This is followed by

kyoto = load('/home/mnair/projects/realtime_mag/KyotoDst19992012.txt');

K_dst_mat = kyoto(:,4:end);
K_dst_ts = reshape(K_dst_mat',[1,4992*24]);
k_unixt = ((datenum(1999,1,1,0,0,0) : 1/24 : datenum(2012,08,31,23,0,0))  - datenum(1970,1,1,0,0,0) ) *24*3600;
unixt = ((datenum(1999,1,1,0,0,0) : 1/24 : datenum(2026,12,31,23,0,0)) - datenum(1970,1,1,0,0,0))*24*3600;

L = K_dst_ts == 9999;
K_dst_ts(L) = 999999;

K_dst_ts(~L) = K_dst_ts(~L) * 100; % To preserve the decimal place when converting it to integer

k_dst_data = ones([1,length(unixt)])*999999;
k_dst_data(1:length(K_dst_ts)) = K_dst_ts;


fid = fopen('/home/mnair/projects/realtime_mag/Dst_1999_2026_Kyoto.bin','wb');
fwrite(fid,int32(k_dst_data),'int32');
fclose(fid);

fid = fopen('/home/mnair/projects/realtime_mag/Dst_1999_2026_time_Kyoto.bin','wb');
fwrite(fid,int32(unixt),'int32');
fclose(fid);


% USGS Dst data

%usgs = load('/home/mnair/projects/realtime_mag/Dst_USGS_1995_2007');
% The new file Jenn made 1985-01-01-01-00-00 to 2009-12-31-00-00-00
% The last dst & other values are not good

%usgs_n = load('/home/mnair/projects/realtime_mag/Dst_def_hourave.out');
 %usgs_n = load('/home/mnair/projects/realtime_mag/Dst_1985-2012_only_data.txt');
 usgs_n =  load('/home/mnair/projects/realtime_mag/Dst_1985-2012.txt');


u_unixt = ((datenum(1985,1,1,1,0,0) : 1/24 : datenum(2012,08,23,19,0,0)) - datenum(1970,1,1,0,0,0))*24*3600;
unixt = ((datenum(1999,1,1,0,0,0) : 1/24 : datenum(2026,12,31,23,0,0)) - datenum(1970,1,1,0,0,0))*24*3600;


startindex = length((datenum(1985,1,1,1,0,0) : 1/24 : datenum(1998,12,31,23,0,0))) + 1;
endindex = length((datenum(1999,1,1,0,0,0) : 1/24 : datenum(2012,08,23,19,0,0))) ;


u_dst_data = ones([1,length(unixt)])*999999;
u_dst_data(1:endindex) = usgs_n(startindex:end,3) * 100 ;

fid = fopen('/home/mnair/projects/realtime_mag/Dst_1999_2026.bin','wb');
fwrite(fid,int32(u_dst_data),'int32');
fclose(fid);

fid = fopen('/home/mnair/projects/realtime_mag/Dst_time.bin','wb');
fwrite(fid,int32(unixt),'int32');
fclose(fid);

omni_data = load('/home/mnair/projects/realtime_mag/OMNI_1999_2012_IMF_Dst_f107');

o_unixt = ((datenum(1999,1,1,0,0,0) : 1/24 : datenum(2012,07,29,23,0,0))  - datenum(1970,1,1,0,0,0) ) *24*3600;
unixt = ((datenum(1999,1,1,0,0,0) : 1/24 : datenum(2026,12,31,23,0,0)) - datenum(1970,1,1,0,0,0))*24*3600;

% 4 Scalar B, nT
% 5 BX, nT (GSE, GSM)
% 6 BY, nT (GSM)
% 7 BZ, nT (GSM)
% 8 SW Plasma Speed, km/s
% 9 Dst-index, nT
% 10 f10.7_index

% IMF time


fid = fopen('/home/mnair/projects/realtime_mag/IMFtime.bin','wb');
fwrite(fid,int32(unixt),'int32');
fclose(fid);



% Bx

imf_bx_data = ones([1,length(unixt)])* 999999;
imf_bx_omni = omni_data(:,5);

K = max(omni_data(:,5));
L = omni_data(:,5) == K;

imf_bx_omni(L) = 999999;
imf_bx_omni(~L) = omni_data(~L,5) * 100;

imf_bx_data(1:119016) = imf_bx_omni;

fid = fopen('/home/mnair/projects/realtime_mag/IMF_1999_2026_Bx.bin','wb');
fwrite(fid,int32(imf_bx_data),'int32');
fclose(fid);

% By


imf_by_data = ones([1,length(unixt)])* 999999;
imf_by_omni = omni_data(:,6);

K = max(omni_data(:,6));
L = omni_data(:,6) == K;

imf_by_omni(L) = 999999;
imf_by_omni(~L) = omni_data(~L,6) * 100;

imf_by_data(1:119016) = imf_by_omni;


fid = fopen('/home/mnair/projects/realtime_mag/IMF_1999_2026_By.bin','wb');
fwrite(fid,int32(imf_by_data),'int32');
fclose(fid);

% Bz

imf_bz_data = ones([1,length(unixt)])* 999999;
imf_bz_omni = omni_data(:,7);

K = max(omni_data(:,7));
L = omni_data(:,7) == K;

imf_bz_omni(L) = 999999;
imf_bz_omni(~L) = omni_data(~L,7) * 100;

imf_bz_data(1:119016) = imf_bz_omni;

fid = fopen('/home/mnair/projects/realtime_mag/IMF_1999_2026_Bz.bin','wb');
fwrite(fid,int32(imf_bz_data),'int32');
fclose(fid);

% Sw


imf_sw_data = ones([1,length(unixt)])* 999999;
imf_sw_omni = omni_data(:,8);

K = max(omni_data(:,8));
L = omni_data(:,8) == K;

imf_sw_omni(L) = 999999;
imf_sw_omni(~L) = omni_data(~L,8) * 100;

imf_sw_data(1:119016) = imf_sw_omni;


fid = fopen('/home/mnair/projects/realtime_mag/IMF_1999_2026_SwV.bin','wb');
fwrite(fid,int32(imf_sw_data),'int32');
fclose(fid);


% F107

f107_unxit = ((datenum(1999,1,1,12,0,0) : 1 : datenum(2026,12,31,12,0,0)) - datenum(1970,1,1,0,0,0))*24*3600;
o_f107 = omni_data(1:24:end,10);

f107_data = ones([1,length(f107_unxit)])* 999999;

K = max(omni_data(1:24:end,10));
L = omni_data(1:24:end,10) == K;


o_f107(L) = 999999;
o_f107(~L) =o_f107(~L) * 100;

f107_data(1:4959) = o_f107;


fid = fopen('/home/mnair/projects/realtime_mag/F107_time.bin','wb');
fwrite(fid,int32(f107_unxit),'int32');
fclose(fid);

fid = fopen('/home/mnair/projects/realtime_mag/F107_1999_2026.bin','wb');
fwrite(fid,int32(f107_data),'int32');
fclose(fid);

